'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.java.HashMap import HashMap
from base.java.LinkHashMap import LinkHashMap
from base.rdf.RDFWriterF import RDFWriterF
from base.shared.JenaException import JenaException

#import java.util.LinkedHashMap;
#import java.util.Map;

#import org.apache.jena.atlas.logging.Log;
#import org.apache.jena.rdf.model.RDFWriterI;
#import org.apache.jena.rdf.model.RDFWriterF;
#import org.apache.jena.shared.JenaException;
#import org.apache.jena.shared.NoWriterForLangException;

'''
 *
'''
class RDFWriterFImpl(RDFWriterF):

    # public static final String
    DEFAULTLANG = "RDF/XML"
    # param 
    # result static Map<String, Class<? extends RDFWriterI>> custom = new LinkedHashMap<>(self)
    custom = LinkHashMap()

    # private static RDFWriterF
    rewiredAlternative = None

    '''
     * Rewire to use an external RDFWriterF (typically, RIOT).
     * Set to null to use old jena-core setup.
     *
     * @param other
    '''
    # param RDFWriterF other
    # result static void
    @staticmethod
    def alternative(other):
        RDFWriterFImpl.rewiredAlternative = other
    

    '''
     * Return the the current "rewiredAlternative" which may be null, meaning:@code RDFWriterFImplis in use.
    '''
    # param RDFWriterF other
    # result static RDFWriterF
    @staticmethod
    def getCurrentRDFWriterF(other):
        return RDFWriterFImpl.rewiredAlternative
    

    '''
     * Creates RDFReaderFImpl
    '''
    # param 
    def __init__(self):
        self.reset()
    

    # @Override
    # param 
    # result RDFWriterI
    def getWriter(self):
        return self.getWriterWithLang(RDFWriterFImpl.DEFAULTLANG)
    

    # @Override
    # param String lang
    # result RDFWriterI
    def getWriterWithLang(self, lang):
        # If RIOT ->
        if RDFWriterFImpl.rewiredAlternative is not None:
            return RDFWriterFImpl.rewiredAlternative.getWriter(lang)
        if lang is None or lang.equals(""):
            lang = RDFWriterFImpl.DEFAULTLANG
        # Class<? extends RDFWriterI>
        c = self.custom.get(lang)
        if c is None:
            raise JenaException("Writer not found: " + lang)
        try:
            return c.getConstructor().newInstance()
        except Exception as e:
            raise JenaException(e)
        
    

    # static:
    #     reset()
    

    # param 
    # result static void
    def reset(self):
        pass
        # TODO
        # Class<? extends RDFWriterI> rdfxmlWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Basic.class
        # Class<? extends RDFWriterI> rdfxmlAbbrevWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Abbrev.class
        # Class<? extends RDFWriterI> ntWriter = org.apache.jena.rdf.model.impl.NTripleWriter.class
        #
        # custom.put("RDF/XML", rdfxmlWriter)
        # custom.put("RDF/XML-ABBREV", rdfxmlAbbrevWriter)
        #
        # custom.put("N-TRIPLE", ntWriter)
        # custom.put("N-TRIPLES", ntWriter)
        # custom.put("N-Triples", ntWriter)
    

    # param String lang
    # result static String
    @staticmethod
    def currentEntry(lang):
        # Class<? extends RDFWriterI>
        oldClass = RDFWriterFImpl.custom.get(lang)
        if (oldClass is not None):
            return oldClass.getName()
        else:
            return None
    

    # param String lang
    # result static String
    @staticmethod
    def remove(lang):
        if RDFWriterFImpl.rewiredAlternative is not None:
            pass
            # Log.error(RDFWriterFImpl.class, "Rewired RDFWriterFImpl2 - configuration changes have no effect on writing")
        # String
        oldClassName = RDFWriterFImpl.currentEntry(lang)
        RDFWriterFImpl.custom.remove(lang)
        return oldClassName
    

